#include "copyright.h"

#include "syscall.h"
#include "Technician.h"


void finishedAgent(const char* name, int length){

	if(GetMonitor(ManagerFinishedName, ManagerFinishedNameLength, 0) == 1){
		ReleaseLock(name, length);
		Exit(1);
	}

}

int technicianInitialize(){

	int myIndex;

	AcquireLock(technicianIndexLockName, technicianIndexLockNameLength);
		myIndex = GetMonitor(nextTechnicianName, nextTechnicianNameLength, 0);
		SetMonitor(nextTechnicianName, nextTechnicianNameLength, myIndex + 1, 0);
		
	ReleaseLock(technicianIndexLockName,technicianIndexLockNameLength);
	
	return myIndex;
}

void technician(){ 

/* doesnt really need an index but I put it because its necessary for forking and this will make it scalable if we want more technicians */
	
	int myIndex;
	int i;
	int tmp;

	myIndex = technicianInitialize();
	
	while(1){
		/*Printing("Technician %i\n", ConsoleOutput, myIndex, 0);*/
		/* 
		A.	if movie conditions met, start movie
		B.	tell customers to leave room
		C.	wait for next movie data
		*/
		/*printf("\t\t\t\tMT: about to acquire the movie lock\n");*/
		AcquireLock(movieDataLockName, movieDataLockNameLength);
		/*printf("\t\t\t\tMT: acquired the movie lock, trying to acquire movie room lock\n");*/
		
		/* A. if movie conditions met, start movie
			1.	check if current time is movie start time
			2.	check if all sold tickets are accounted for
		*/
		AcquireLock(movieRoomLockName, movieRoomLockNameLength);
		/*printf("\t\t\t\tMT: acquired the movie room lock\n");*/
		
		/*printf("\t\t\t\tMT: checking if I can play movie\n");*/
		if(GetMonitor(movieStartTimeName, movieStartTimeNameLength,0) == 1 && GetMonitor(seatsFilledName, seatsFilledNameLength, 0) == GetMonitor(numberAdmittedName, numberAdmittedNameLength, 0)){ /* remove the && bit later */
			
			SetMonitor(numberAdmittedName, numberAdmittedNameLength, 0, 0);

			tmp = Random() % 100 + 200;
			SetMonitor(movieLengthName, movieLengthNameLength, tmp, 0);
			SetMonitor(movieLengthName, movieLengthNameLength, 300, 0);
			
			
			Printing("The MovieTechnician has started the movie.\n", ConsoleOutput, 0, 0);
			
		/*	printf("\t\t\t\tMT: playing dat movie, I love Matt Damon. its %i long\n", movieLength);*/
			for(i = 0; i < GetMonitor(movieLengthName, movieLengthNameLength, 0); i++){
				/*currentThread->Yield();*/ /* used to play movie */
				Yield();
			}
			
			
			
			/* movie should be done at this point */
			SetMonitor(seatsFilledName, seatsFilledNameLength, 0, 0);
			SetMonitor(movieStartTimeName, movieStartTimeNameLength, 0, 0);
			SetMonitor(movieEndedName, movieEndedNameLength, 1, 0);
			
			/* B. tell customers to leave room */
			Printing("The MovieTechnician has ended the movie.\n", ConsoleOutput, 0, 0);
			
			for(i = 0; i < NUM_ROWS; i++){
			
				SetMonitor(roomName[0], roomNameLength[0], 0, i);
				
			}
			Broadcast(movieRoomConditionVariableName,movieRoomConditionVariableNameLength, movieRoomLockName,  movieRoomLockNameLength);
			Printing("The MovieTechnician has told all customers to leave the theater room.\n", ConsoleOutput, 0, 0);
		}
		/*printf("\t\t\t\tMT: about to release movie room lock\n");*/
		ReleaseLock(movieRoomLockName, movieRoomLockNameLength);
		/* C. wait for next movie data */
		/*printf("\t\t\t\tMT: released movie room lock, waiting for my movie data\n");*/
		Wait(technicianDataConditionVariableName,technicianDataConditionVariableNameLength, movieDataLockName, movieDataLockNameLength);
		/*printf("\t\t\t\tMT: reacquired and now about to release movie data lock\n");*/
		finishedAgent(movieDataLockName, movieDataLockNameLength);
		ReleaseLock(movieDataLockName, movieDataLockNameLength);
		/*printf("\t\t\t\tMT: released movie data lock\n");*/
	}
	
}


int main(){

	technician();

	Exit(1);
}